package cn.iocoder.yudao.module.energy.controller.admin.device;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.energy.controller.admin.device.vo.EnergyDeviceCreateReqVO;
import cn.iocoder.yudao.module.energy.controller.admin.device.vo.EnergyDeviceExportVO;
import cn.iocoder.yudao.module.energy.controller.admin.device.vo.EnergyDevicePageReqVO;
import cn.iocoder.yudao.module.energy.controller.admin.device.vo.EnergyDeviceRespVO;
import cn.iocoder.yudao.module.energy.controller.admin.device.vo.EnergyDeviceUpdateReqVO;
import cn.iocoder.yudao.module.energy.convert.device.EnergyDeviceConvert;
import cn.iocoder.yudao.module.energy.dal.dataobject.device.EnergyDeviceDO;
import cn.iocoder.yudao.module.energy.service.device.EnergyDeviceService;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 能耗设备")
@RestController
@RequestMapping("/energy/device")
@Validated
public class EnergyDeviceController {

    @Resource
    private EnergyDeviceService energyDeviceService;

    @PostMapping("/create")
    @Operation(summary = "创建能耗设备")
    @PreAuthorize("@ss.hasPermission('energy:device:create')")
    public CommonResult<Long> createEnergyDevice(@Valid @RequestBody EnergyDeviceCreateReqVO createReqVO) {
        return success(energyDeviceService.createEnergyDevice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新能耗设备")
    @PreAuthorize("@ss.hasPermission('energy:device:update')")
    public CommonResult<Boolean> updateEnergyDevice(@Valid @RequestBody EnergyDeviceUpdateReqVO updateReqVO) {
        energyDeviceService.updateEnergyDevice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除能耗设备")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('energy:device:delete')")
    public CommonResult<Boolean> deleteEnergyDevice(@RequestParam("id") Long id) {
        energyDeviceService.deleteEnergyDevice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得能耗设备")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('energy:device:query')")
    public CommonResult<EnergyDeviceRespVO> getEnergyDevice(@RequestParam("id") Long id) {
        EnergyDeviceDO energyDevice = energyDeviceService.getEnergyDevice(id);
        return success(EnergyDeviceConvert.INSTANCE.convert(energyDevice));
    }

    @GetMapping("/page")
    @Operation(summary = "获得能耗设备分页")
    @PreAuthorize("@ss.hasPermission('energy:device:query')")
    public CommonResult<PageResult<EnergyDeviceRespVO>> getEnergyDevicePage(@Valid EnergyDevicePageReqVO pageVO) {
        PageResult<EnergyDeviceDO> pageResult = energyDeviceService.getEnergyDevicePage(pageVO);
        return success(EnergyDeviceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/list")
    @Operation(summary = "获得能耗设备列表")
    @PreAuthorize("@ss.hasPermission('energy:device:query')")
    public CommonResult<List<EnergyDeviceRespVO>> getEnergyDeviceList() {
        List<EnergyDeviceDO> list = energyDeviceService.getEnergyDeviceList();
        return success(EnergyDeviceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/export")
    @Operation(summary = "导出能耗设备")
    @PreAuthorize("@ss.hasPermission('energy:device:export')")
    public void exportEnergyDevice(@Valid EnergyDevicePageReqVO pageVO, HttpServletResponse response) throws IOException {
        // 查询数据
        List<EnergyDeviceDO> list = energyDeviceService.getEnergyDeviceList(pageVO);
        
        // 转换为导出格式
        List<EnergyDeviceExportVO> exportList = EnergyDeviceConvert.INSTANCE.convertList03(list);
        
        // 导出Excel
        ExcelUtils.write(response, "能耗设备数据.xlsx", "数据", EnergyDeviceExportVO.class, exportList);
    }
} 